package org.jeecg.modules.demo.sermanager.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.google.gson.JsonObject;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.RestUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.sermanager.entity.NetptSer;
import org.jeecg.modules.demo.sermanager.entity.SysDictItem;
import org.jeecg.modules.demo.sermanager.service.INetptSerService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.demo.sermanager.service.ISysDictItemService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: netpt_ser
 * @Author: jeecg-boot
 * @Date:   2022-01-07
 * @Version: V1.0
 */
@Api(tags="服务管理")
@RestController
@RequestMapping("/sermanager/netptSer")
@Slf4j
public class NetptSerController extends JeecgController<NetptSer, INetptSerService> {
	@Autowired
	private INetptSerService netptSerService;

	 @Autowired
	 private ISysDictItemService iSysDictItemService;

	 @ApiOperationSupport(order = 1)
	 @AutoLog(value = "service/version 字典查询")
	 @ApiOperation(value="service/version 字典查询", notes="service/version 字典查询")
	 @GetMapping(value = "/dictList")
	 public Result<?> queryDictItemList(){
		 Map map=new HashMap();
		 List serList=iSysDictItemService.selectItemsByMainId("1478539688244199425");
		 List verList=iSysDictItemService.selectItemsByMainId("1478540135000489985");
		 SysDictItem sysDictItem=iSysDictItemService.getById("1479342586561609729");

		 map.put("serveList",serList);
		 map.put("versionsList",verList);
		 map.put("containerNum",sysDictItem.getItemValue());
		 return Result.OK(map);
	 }


	 @ApiOperationSupport(order = 2)
	 @AutoLog(value = "修改容器数量")
	 @ApiOperation(value="修改容器数量", notes="修改容器数量")
	 @PutMapping(value = "/updateContainerNum")
	 public Result<?> updateContainNum(@RequestParam(name="num",required=true) String num){
		 SysDictItem entity=new SysDictItem();
		 entity.setId("1479342586561609729");
		 entity.setItemValue(num);
		 iSysDictItemService.updateById(entity);
		 //调用创建容器接口
		 JSONObject jsonObject=new JSONObject();
		 jsonObject.put("gs",num);
		 RestUtil.post("",jsonObject);
		 return Result.OK();
	 }


	/**
	 * 分页列表查询
	 *
	 * @param netptSer
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "netpt_ser-分页列表查询")
	@ApiOperation(value="netpt_ser-分页列表查询", notes="netpt_ser-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(NetptSer netptSer,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) throws Exception{
		BeanUtils.populate(netptSer, req.getParameterMap());
		QueryWrapper<NetptSer> queryWrapper=new QueryWrapper();
		if(StringUtils.isNotBlank(netptSer.getServe())){
			queryWrapper.like("serve",netptSer.getServe());
		}
		if(StringUtils.isNotBlank(netptSer.getVersions())){
			queryWrapper.like("versions",netptSer.getVersions());
		}
		Page<NetptSer> page = new Page<NetptSer>(pageNo, pageSize);
		IPage<NetptSer> pageList = netptSerService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param netptSer
	 * @return
	 */
	@AutoLog(value = "netpt_ser-添加")
	@ApiOperation(value="netpt_ser-添加", notes="netpt_ser-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody NetptSer netptSer) {
		QueryWrapper<NetptSer> queryWrapper=new QueryWrapper();
		queryWrapper.like("serve",netptSer.getServe());
		queryWrapper.like("versions",netptSer.getVersions());
		NetptSer entity=netptSerService.getOne(queryWrapper);
		if(entity!=null){
			return Result.error("该服务版本已存在！");
		}
		netptSerService.save(netptSer);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param netptSer
	 * @return
	 */
	@AutoLog(value = "netpt_ser-编辑")
	@ApiOperation(value="netpt_ser-编辑", notes="netpt_ser-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody NetptSer netptSer) {
		netptSerService.updateById(netptSer);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "netpt_ser-通过id删除")
	@ApiOperation(value="netpt_ser-通过id删除", notes="netpt_ser-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		netptSerService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "netpt_ser-批量删除")
	@ApiOperation(value="netpt_ser-批量删除", notes="netpt_ser-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.netptSerService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "netpt_ser-通过id查询")
	@ApiOperation(value="netpt_ser-通过id查询", notes="netpt_ser-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		NetptSer netptSer = netptSerService.getById(id);
		if(netptSer==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(netptSer);
	}



	 /**
	  *
	  *服务名称列表查询（创建任务时）
	  */
	 @AutoLog(value = "服务名称列表查询")
	 @ApiOperation(value="服务名称列表查询", notes="服务名称列表查询")
	 @GetMapping(value = "/querySerlist")
	 public Result<?> querySerNameList(NetptSer netptSer, HttpServletRequest req) {
		 QueryWrapper<NetptSer> queryWrapper = QueryGenerator.initQueryWrapper(netptSer, req.getParameterMap());
		 List<NetptSer> list = netptSerService.querySerNamelist();
		 return Result.OK(list);
	 }



	 /**
	  *
	  *根据服务名称查询版本列表（创建任务时）
	  */
	 @AutoLog(value = "根据服务名称查询版本列表")
	 @ApiOperation(value="根据服务名称查询版本列表", notes="根据服务名称查询版本列表")
	 @GetMapping(value = "/queryVersionslist")
	 public Result<?> queryVersionslist(NetptSer netptSer, HttpServletRequest req) throws Exception{
		 BeanUtils.populate(netptSer,req.getParameterMap());
		 List<NetptSer> list = netptSerService.queryVersionslist(netptSer.getServe());
		 return Result.OK(list);
	 }



    /**
    * 导出excel
    *
    * @param request
    * @param netptSer
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, NetptSer netptSer) {
        return super.exportXls(request, netptSer, NetptSer.class, "netpt_ser");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, NetptSer.class);
    }

}
